#include "StdAfx.h"
// #include "CValueTableStr.h"  // precompiled header

CValueTableStr::CValueTableStr()
{
}

CValueTableStr::~CValueTableStr()
{
}


#ifdef _DEBUG
void CValueTableStr::AssertValid() const
{
    ASSERT(mstr_name.GetCount()==mstr_value.GetCount());
}
#endif //_DEBUG


CValueTableStr& CValueTableStr::operator=(const CValueTableStr& rs_data)
{
    mstr_name.RemoveAll();
    mstr_value.RemoveAll();

    mstr_name.Append(rs_data.mstr_name);
    mstr_value.Append(rs_data.mstr_value);
    return *this;
}

bool CValueTableStr::GetIDPair(int id, CString& name, CString& value)
{
    if (id<0 || id >= mstr_name.GetCount()) return false;

    name = mstr_name[id];
    value = mstr_value[id];

    return true;
}



const CString&  CValueTableStr::GetValue(const CString&spec, const CString& def/*=0*/)
{
    int sp;
    for (sp = 0; sp<mstr_name.GetCount();sp++)
        if (mstr_name[sp] == spec) break;

    if (sp<mstr_name.GetCount())
        return mstr_value[sp];

    return def;
}

BOOL CValueTableStr::Exists(const CString&spec)
{
    int sp;
    for (sp = 0; sp<mstr_name.GetCount();sp++)
        if (mstr_name[sp] == spec) return TRUE;

    return FALSE;
}

const CString&  CValueTableStr::GetValueExists(const CString&spec)
{
    int sp;
    for (sp = 0; sp<mstr_name.GetCount();sp++)
        if (mstr_name[sp] == spec) break;

    ASSERT(sp<mstr_name.GetCount());

    return mstr_value[sp];
}



void CValueTableStr::SetValue(const CString& spec, const CString& val)
{
    int sp;
    for (sp = 0; sp<mstr_name.GetCount();sp++)
        if (mstr_name[sp] == spec) break;

    ASSERT(sp<mstr_name.GetCount());

    mstr_value[sp] = val;
}

void CValueTableStr::SetValueAndAdd(const CString&spec, const CString& val)
{
    int sp;
    for (sp = 0; sp<mstr_name.GetCount();sp++)
        if (mstr_name[sp] == spec) break;

    if (sp<mstr_name.GetCount()) mstr_value[sp] = val;
    else
    {
        mstr_name.Add(spec);
        mstr_value.Add(val);
    }
}


void CValueTableStr::SetValueDelete(const CString& spec, const CString& val)
{
    int sp;
    for (sp = 0; sp<mstr_name.GetCount();sp++)
        if (mstr_name[sp] == spec) break;

    if (val.IsEmpty())
    {
        if (sp>=mstr_name.GetCount()) return;

        mstr_name.RemoveAt(sp);
        mstr_value.RemoveAt(sp);
        return;
    }

    if (sp<mstr_name.GetCount()) mstr_value[sp] = val;
    else
    {
        mstr_name.Add(spec);
        mstr_value.Add(val);
    }
}

void CValueTableStr::Append(const CStringArray&in)
{
    mstr_name.Append(in);

    while (mstr_name.GetCount()>mstr_value.GetCount())
        mstr_value.Add(0);
}


void CValueTableStr::DeleteContents()
{
    mstr_name.RemoveAll();
    mstr_value.RemoveAll();
}


bool CValueTableStr::Load(FILE*inf)
{
    BYTE dw_count;
    if (1!=fread(&dw_count,sizeof(BYTE),1,inf)) return false;

    mstr_name.RemoveAll();
    mstr_value.RemoveAll();

    TRACE(2,"reading %i values",dw_count);
    for (DWORD t=0;t<dw_count;t++)
    {
        mstr_value.Add(fReadString(inf));
        mstr_name.Add(fReadString(inf));
    }
    return true;
}

bool CValueTableStr::Save(FILE*inf)
{
    ASSERT(mstr_name.GetCount()<255); // disable 'cause not used at the moment...additional fix it in load
    BYTE dw_count = (BYTE)mstr_name.GetCount();
    fwrite(&dw_count,sizeof(BYTE),1,inf);

    for (DWORD t=0;t<dw_count;t++)
    {
        fWriteString(inf, mstr_value[t]);
        fWriteString(inf, mstr_name[t]);
    }
    return true;
}

void CValueTableStr::fWriteString(FILE*f, CString& str)
{
    DWORD dw_len = str.GetLength();
    fwrite(&dw_len,sizeof(DWORD),1,f);
    fwrite(str.GetBuffer(),dw_len,1,f);
}

CString CValueTableStr::fReadString(FILE*f)
{
    DWORD dw_len;
    CString str;

    fread(&dw_len,sizeof(DWORD),1,f);
    fread(str.GetBuffer(dw_len+1),dw_len,1,f);
    str.GetBuffer()[dw_len]=0;
    str.ReleaseBuffer();

    return str;
}